<?php
    /* Fingon 2011.07 */
    require_once('global.php');
    require_once('/code/SiteInfo.php');
    require_once('/code/Include/db_mysql.php');
    require_once('/code/Include/thumb_handler.php');
    
    Class News
    {
        protected $db;
        
        const IMG_MAX_W=300;
        const IMG_MAX_H=450;
        const IMG_WATERMARK_OFFSET_X=10;
        const IMG_WATERMARK_OFFSET_Y=-2;
        const WATERMARK_FONT_SIZE=10;
        const IMG_LOCATION='/news/images/';
        const WATERMARK_FONT='./code/Include/ariblk.ttf';
        
        public function News(){
            $this->db=new DB();
        }
        
        public function getNews($only_active=1){
            $sql='select * from `news`'.($only_active?' where `is_active`=1':'').' order by create_time DESC';
            return $this->db->get_rows($sql);
        }
        
        public function getNewsById($nid){
            $nid=intval($nid);
            $sql="select * from `news` where news_id=$nid";
            return $this->db->get_one($sql);
        }
        
        public function addNews($n){
            $n=$this->db->prepare_param($n);
            $sql=sprintf("insert into `news` (`create_time`, `is_active`, `caption`, `content`, `abstract`) values (now(), %s, '%s', '%s', '%s')", $n['is_active'], $n['caption'], $n['content'], $n['abstract']);
            $this->db->update($sql);
            return $this->db->insert_id();
        }

        public function editNews($n){
            $n=$this->db->prepare_param($n);
            $sql=sprintf("update `news` set `is_active`=%s, `caption`='%s', `content`='%s', `abstract`='%s' where news_id=%s", $n['is_active'], $n['caption'], $n['content'], $n['abstract'], $n['news_id']);
            return $this->db->update($sql);
        }
        
        public function deleteNews($nid){
            $sql="select img_url from `news` where `news_id` = $nid";
            $rs=$this->db->get_one($sql);
            $sql="delete from `news` where `news_id` = $nid";
            $this->db->update($sql);
            if($this->db->affected_rows()>0) {
                @unlink('.'.$rs['img_url']);
                return true;        
            }
            else return false;
        }
        
        public function uploadNewsImg($nid, $imgFile){
            $tmp_url=$imgFile['tmp_name'];
            $sql="select `img_url` from `news` where news_id=$nid";
            $rs=$this->db->get_one($sql);
            $old_img=$rs['img_url']; 
            if($old_img!='') {$tmp_img=$old_img.'.tmp'; @rename('.'.$old_img, '.'.$tmp_img);}
            try{
                $t=new ThumbHandler();
                $siteInfo=new SiteInfo();
                $w=$siteInfo->getWartermark();
                $t->setMaskWord($w['watermark_text']);
                $t->setMaskFont(self::WATERMARK_FONT);
                $t->setMaskTxtPct($w['watermark_transparency']);
                //image
                $t->setSrcImg($tmp_url);
                $img_url=self::IMG_LOCATION.'news'.$nid.'.'.$t->img_type;
                $t->setDstImg('.'.$img_url);
                $t->setMaskFontSize(self::WATERMARK_FONT_SIZE);
                $t->setMaskOffsetX(self::IMG_WATERMARK_OFFSET_X);
                $t->setMaskOffsetY(self::IMG_WATERMARK_OFFSET_Y);
                $t->createImg(self::IMG_MAX_W,self::IMG_MAX_H);
                @unlink($tmp_url);
                if($old_img!='') @unlink('.'.$tmp_img);
            }
            catch(Exception $e){
                @unlink($tmp_url);
                if($old_img!='') @rename('.'.$tmp_img, '.'.$old_img);
                throw $e;
            }
            $sql="update `news` set `img_url`='$img_url' where news_id=$nid";
            $this->db->update($sql);
            return $img_url;
        }
        
         public function deleteNewsImg($nid){
            $sql="select `img_url` from `news` where news_id=$nid";
            $rs=$this->db->get_one($sql);
            $img_url=$rs['img_url'];
            if($img_url=='') return false;
            @unlink('.'.$img_url);
            $sql="update `news` set `img_url`='' where news_id=$nid";
            return $this->db->update($sql);
         }
    }
?>